
	mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					up: {
						auto:false,
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
	});
	
	
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var currenPage=1;
    var pageSize=10;
    var total_page=0;
    var total_count=0;
    
    window.onload = function(){
    	mui.ajax({
				url: apiurl + "Store/my_orders/",
				data: {'p':currenPage,'size':pageSize,'user_id':uid,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						total_page=res.total_page;
						total_count=res.sum;
						var htm=get_data(res.resource);
						$('#pullrefresh .mui-scroll .mui-table-view').append(htm);
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
			
			mui.plusReady(function(){
			    //关闭等待框
			    plus.nativeUI.closeWaiting();
			    //显示当前页面
			    mui.currentWebview.show();
			    
			    document.getElementById('cancel').addEventListener('tap', function() {
					document.getElementById('order_id').value='';
					document.getElementById('shipping_info').style.display="none";
				})
			    
			     document.getElementById('do_receive').addEventListener('tap', function() {
					var Info={
						'user_id':uid,
						'order_id':document.getElementById('order_id').value.trim(),
						'_token':userinfo.token
					};
					if(Info.order_id==''){
						mui.alert('未获取到订单id');
						return false;
					}
					
					mui.ajax({
							url: apiurl + "Store/do_receiving/",
							data: Info,
							async: true,
							dataType: 'json',
							crossDomain: true, //强制使用5+跨域
							type: 'post',
							timeout: 10000,
							success: function(res) {
								console.log(JSON.stringify(res));
								if(res.status ==-3){
									mui.toast(res.msg);
									app.out();
									return false;
								}
								// 请求成功
								if(res.status==0) {
									mui.toast(res.msg);
								} else {
									mui.toast(res.msg);
									setTimeout(function() {
										location.href=location;
									}, 500);
				
								}
							},
							error: function(xhr, type, errorThrown) {
								//console.log(JSON.stringify(xhr));
								//console.log(JSON.stringify(type));
								//console.log(JSON.stringify(errorThrown));
								mui.toast('获取数据失败！');
							}
						});
				})
			    
			     mui(".mui-table-view").on('tap','.mui-table-view-cell .service_tel',function(){
			     	var phone=this.getAttribute('data-mobile');
			     	if(mui.os.plus) {
						plus.device.dial(phone);
					} else {
						location.href = 'tel:' + phone;
					}
			     },'false');
			     
			});

    }

	var j=0;
    function get_data(data){
    	/*
		<li class="mui-table-view-cell">
					<div class="goodsbox">
								<div class="goods-top">
									<div class="goods-top-left">
										<img src="../../../img/shop.png">
									</div>
									<div class="goods-top-right">
										<h3>苹果iPhone7全网通4G手机中国红</h3>
										<p class='mui-ellipsis'>价格：3278</p>
										<p class='mui-ellipsis'>库存：3</p>
									</div>
									<div class="goods-triangle"><p class="goods-num">01</p></div>
									<div class="goods-money">合计：9999</div>
									<div style="clear: both;"></div>
								</div>
								<div class="goods-main">
									<p>订单编号：2018062901</p>
									<p>姓名：张三</p>
									<p>联系电话：2018062901</p>
									<p>收货地址：2018062901</p>
									<p>备注信息：2018062901</p>
								</div>
								<div class="goods-bottom">
									<p>订单编号：2018062901</p>
									<p>姓名：张三</p>
								</div>
								<div class="shop-goods"><button>发货</button><div style="clear: both;"></div></div>
							</div>
							<span class="mui-pull-right" id="confirm" style="display: none;"></span>
						
				</li>
			 */
    	var htm='';
		for(z in data) {
			htm+='<li class="mui-table-view-cell">';
			htm+='<div class="goodsbox">';
			htm+='<div class="goods-top">';
			htm+='<div class="goods-top-left">';
			if(data[z]['original_img'].indexOf(upurl+'Uploads') !== -1){
				var img_path=data[z]['original_img'];
			}
			else{
				var img_path=upurl+data[z]['original_img'];
			}
			htm+='<img src="'+img_path+'">';
			htm+='</div>';
			htm+='<div class="goods-top-right">';
			htm+='<h3>'+data[z]['goods_name']+'</h3>';
			htm+='<p class="mui-ellipsis">价格：'+data[z]['goods_price']+'</p>';
			htm+='<p class="mui-ellipsis">数量：'+data[z]['goods_num']+'</p>';
			htm+='<p class="mui-ellipsis">运费：'+data[z]['shipping_price']+'</p>';
			htm+='</div>';
			j++;
			if(j<10){
				htm+='<div class="goods-triangle"><p class="goods-num">0'+j+'</p></div>';
			}
			htm+='<div class="goods-money">合计：'+data[z]['order_amount']+'</div>';
			htm+='<div style="clear: both;"></div>';
			htm+='</div>';
			
			htm+='<div class="goods-main">';
			htm+='<p>订单编号：'+data[z]['order_sn']+'</p>';
			htm+='<p>下单时间：'+data[z]['add_time']+'</p>';
			if(data[z]['shipping_status']==1){
				htm+='<p>发货时间：'+data[z]['shipping_time']+'</p>';
			}
			
			if(data[z]['shipping_status']==0 && data[z]['order_status']==1){
				htm+='<p>状态：<font style="color:#fab538">未发货</font></p>';
			}
			if(data[z]['shipping_status']==1 && data[z]['order_status']==1){
				htm+='<p>状态：<font style="color:#fab538">已发货</font></p>';
			}
			if(data[z]['shipping_status']==1 && data[z]['order_status']==2){
				htm+='<p>收货时间：'+data[z]['confirm_time']+'</p>';
				htm+='<p>状态：<font style="color:#fab538">已完成</font></p>';
			}
			if(data[z]['order_status']==3){
				htm+='<p>取消时间：'+data[z]['return_time']+'</p>';
				htm+='<p>状态：<font style="color:#fab538">已取消</font></p>';
			}
			
			htm+='<p>店铺名称：'+data[z]['store_name']+'</p>';
			//htm+='<p>商家UID：'+data[z]['sale_uid']+'</p>';
			htm+='<p>商家登录手机：'+data[z]['sal_phone']+'</p>';
			htm+='<p>收货人姓名：'+data[z]['consignee']+'</p>';
			htm+='<p>收货电话：'+data[z]['mobile']+'</p>';
			htm+='<p>收货地址：'+data[z]['province']+' '+data[z]['city']+' '+data[z]['district']+' '+data[z]['address']+'</p>';
			htm+='<p>备注信息：'+data[z]['user_note']+'</p>';
			htm+='<p>客服电话：<a class="service_tel" style="color:#fab538;" data-mobile="'+data[z]['sale_mobile']+'">'+data[z]['sale_mobile']+'</a></p>';
			htm+='</div>';
			if(data[z]['shipping_status']==0 && data[z]['order_status']==1){
				htm+='<div class="shop-goods"><button onclick="del_order(\''+data[z]['order_id']+'\')">取消订单</button><div style="clear: both;"></div></div>'
			}
			
			if(data[z]['shipping_status']==1){
				htm+='<div class="goods-bottom">';
				htm+='<p class="mui-ellipsis">物流公司：'+data[z]['shipping_name']+'</p>';
				htm+='<p class="mui-ellipsis">物流单号：'+data[z]['invoice_no']+'</p>';
				htm+='</div>';
				if(data[z]['shipping_status']==1 && data[z]['order_status']==1){
					htm+='<div class="shop-goods"><button onclick="receiving(\''+data[z]['order_id']+'\')">确认收货</button><div style="clear: both;"></div></div>'
					//htm+='<button class="shop-goods" onclick="receiving(\''+data[z]['order_id']+'\')">确认收货</button>';	
				}
			}
			htm+='</div>';
			htm+='<span class="mui-pull-right" id="confirm" style="display: none;"></span>';
			htm+='</li>';
		}
		return htm;
    }

	function pullupRefresh() {
		setTimeout(function() {
			currenPage++;
			mui.ajax({
				url: apiurl + "Store/my_orders/",
				data: {'p':currenPage,'size':pageSize,'user_id':uid,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						var htm=get_data(res.resource);
						$('#pullrefresh .mui-scroll .mui-table-view').append(htm);
					}
					
					if(currenPage < total_page){
	                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    /*能上拉*/
	                }else{
	                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);/*不能上拉*/
	                }
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
			
		}, 1500);
	}
	
	
	//收货
	function receiving(order_id){
		document.getElementById('order_id').value=order_id;
		document.getElementById('shipping_info').style.display="block";
	}

    //取消订单
    function del_order(order_id){
		var btnArray = ['确定', '取消'];
		mui.confirm('确定要取消该订单吗？', '取消订单' , btnArray, function(e) {
			if(e.index == 0) {
				mui.ajax({
					url: apiurl + "Store/del_order/",
					data: {
						'order_id': order_id,
						'_token':userinfo.token
					},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'get',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}
						// 请求成功
						if(res.status==0) {
							mui.toast(res.msg);
						} else {
							mui.toast(res.msg);
							location.href = location;
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
			} else {
				//
			}
		})
    }
